package cst.a216yzh.widgesttest216yzh;

import android.content.Intent;
import android.support.annotation.IdRes;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity implements View.OnClickListener{
    private EditText inputText;
    private Button textbutton,changebutton,sexbutton,hobbybutton;
    private ImageView imageView;
    private ImageButton imageButton;
    private RadioButton radioButton,radioButton2;
    private RadioGroup rg;
    private CheckBox cblan,cbzu,cbyu;
    private String msg="";

    private void init(){
        inputText=(EditText)findViewById(R.id.inputText);
        textbutton=(Button)findViewById(R.id.textbutton);
        imageView=(ImageView)findViewById(R.id.imageView);
        changebutton=(Button)findViewById(R.id.changebutton);
        imageButton=(ImageButton)findViewById(R.id.imagebutton);
        radioButton=(RadioButton)findViewById(R.id.radioButton);
        radioButton2=(RadioButton)findViewById(R.id.radioButton2);
        sexbutton=(Button) findViewById(R.id.sexbutton);
        rg=(RadioGroup) findViewById(R.id.radiogroup);
        cblan=(CheckBox) findViewById(R.id.cblan);
        cbzu=(CheckBox) findViewById(R.id.cbzu);
        cbyu=(CheckBox) findViewById(R.id.cbyu);
        hobbybutton=(Button) findViewById(R.id.hobbybutton);

    }
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        init();
        textbutton.setOnClickListener(this);
        changebutton.setOnClickListener(this);
        imageButton.setOnClickListener(this);
        sexbutton.setOnClickListener(this);
        hobbybutton.setOnClickListener(this);
        rg.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, @IdRes int checkedId) {
                switch (checkedId){
                    case R.id.radioButton:
                        inputText.setText("男");
                        break;
                    case R.id.radioButton2:
                        inputText.setText("女");
                        break;
                }
            }
        });
        cblan.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if(isChecked)msg+="篮球 ";
                else
                    msg=msg.replace("篮球 ","");
                inputText.setText(msg);

            }
        });
        cbzu.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if(isChecked)msg+="足球 ";
                else
                    msg=msg.replace("足球 ","");
                inputText.setText(msg);

            }
        });
        cbyu.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if(isChecked)msg+="羽毛球 ";
                else
                    msg=msg.replace("羽毛球 ","");
                inputText.setText(msg);

            }
        });
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.textbutton:
                Toast.makeText(this, inputText.getText().toString(), Toast.LENGTH_SHORT).show();
                break;
            case R.id.changebutton:
                imageView.setImageResource(R.drawable.eclipse);
                imageView.setScaleType(ImageView.ScaleType.FIT_CENTER);
                break;
            case R.id.imagebutton:
                Intent intent=new Intent(MainActivity.this,ButtonActivity.class);
                intent.putExtra("para1",inputText.getText().toString());
                startActivity(intent);
                break;
            case R.id.sexbutton:
                if(radioButton.isChecked()) {
                    inputText.setText("男");
                    }
                    else if(radioButton2.isChecked()){
                        inputText.setText("女");
                    }
                break;
            case R.id.hobbybutton:
                String str="";
                if(cblan.isChecked())str+="篮球";
                if(cbzu.isChecked())str+="足球";
                if(cbyu.isChecked())str+="羽毛球";
                inputText.setText(str);
                break;
        }
        }

        }


